package com.pvj.myaccount.uitls

import android.annotation.SuppressLint
import android.os.Build
import androidx.annotation.RequiresApi
import java.text.ParseException
import java.text.SimpleDateFormat
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.util.Calendar
import java.util.Date
import java.util.TimeZone

/**
 * 时间工具类

 */

object TimeUtil {

    fun getCurrentBeijingTimestamp(): Long {
        // 获取当前时间的ZonedDateTime对象，指定时区为北京时间
        val beijingZonedTime = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"))
        // 将ZonedDateTime转换为UTC的Instant
        val instant = beijingZonedTime.toInstant()
        // 获取Instant对应的时间戳
        return instant.toEpochMilli()
    }


    fun convertToDateTimestamp(year: Int, month: Int, day: Int): Long {
        // 创建指定年月日的LocalDate对象
        val date = LocalDate.of(year, month, day)
        // 将LocalDate转换为LocalDateTime，时间为当天的开始00:00:00
        val localDateTime = date.atStartOfDay()
        // 转换为北京时间的ZonedDateTime
        val zoneIdBeijing = ZoneId.of("Asia/Shanghai") // 指定时区为北京时间
        val zonedDateTime = localDateTime.atZone(zoneIdBeijing)
        // 获取ZonedDateTime对应的时间戳
        return zonedDateTime.toInstant().toEpochMilli()
    }

    /**
     * 毫秒数转成yyyy-MM-dd HH:mm:ss字符串
     * @param timeMillis
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    fun timestampToStr(timeMillis: Long): String {
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = timeMillis
        //val format = "yyyy-MM-dd HH:mm:ss"
        val format = "yyyy-MM-dd HH:mm"
        val sdf = SimpleDateFormat(format)
        return sdf.format(calendar.time)
    }

    fun timestampToBeijingStr(timestamp: Long): String {
        // 将时间戳转换为Instant
        val instant = Instant.ofEpochMilli(timestamp)

        // 将Instant转换为北京时间（Asia/Shanghai）的LocalDateTime
        val localDateTime = LocalDateTime.ofInstant(instant, ZoneId.of("Asia/Shanghai"))

        // 定义日期格式
        val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")

        // 使用定义的格式格式化日期
        return localDateTime.format(formatter)
    }

    fun timestampToBeijingDay(timestamp: Long): Int {
        // 将时间戳转换为Instant
        val instant = Instant.ofEpochMilli(timestamp)

        // 将Instant转换为北京时间（Asia/Shanghai）的LocalDateTime
        val localDateTime = LocalDateTime.ofInstant(instant, ZoneId.of("Asia/Shanghai"))

        // 使用定义的格式格式化日期
        return localDateTime.dayOfMonth
    }

    fun timestampToBeijingMonth(timestamp: Long): Int {
        // 将时间戳转换为Instant
        val instant = Instant.ofEpochMilli(timestamp)

        // 将Instant转换为北京时间（Asia/Shanghai）的LocalDateTime
        val localDateTime = LocalDateTime.ofInstant(instant, ZoneId.of("Asia/Shanghai"))

        // 使用定义的格式格式化日期
        return localDateTime.monthValue
    }

    fun getLastDayOfMonthTimestamp(year: Int, month: Int): Long {
        // 获取该月的最后一天
        val lastDayOfMonth = LocalDate.of(year, month, 1).plusMonths(1).withDayOfMonth(1).minusDays(1)
        // 设置时间为月末的23:59:59
        val endOfDay = LocalDateTime.of(lastDayOfMonth, LocalTime.MAX)

        // 转换为北京时间的ZonedDateTime
        val instant = endOfDay.atZone(ZoneId.of("Asia/Shanghai")).toInstant()

        // 获取Instant对应的时间戳
        return instant.toEpochMilli()
    }

    fun getFirstDayOfMonthTimestamp(year: Int, month: Int): Long {
        // 创建代表该月1号的LocalDate对象
        val firstDayOfMonth = LocalDate.of(year, month, 1)
        // 转换为北京时区的ZonedDateTime对象，并设置时间为00:00:00
        val firstDayOfMonthBeijing = firstDayOfMonth.atStartOfDay(ZoneId.of("Asia/Shanghai"))
        // 获取ZonedDateTime对应的时间戳
        return firstDayOfMonthBeijing.toInstant().toEpochMilli()
    }


    // 获取今日北京时间的起始时间戳
    fun getCurrentDayStartBeijingTimestamp(): Long {
        // 获取当前时间的ZonedDateTime对象，指定时区为北京时间
        val now = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"))

        // 创建一个表示今天凌晨0点的ZonedDateTime对象
        val startOfToday = ZonedDateTime.of(now.toLocalDate(), LocalTime.MIDNIGHT, ZoneId.of("Asia/Shanghai"))

        // 将ZonedDateTime转换为UTC的Instant
        val instant = startOfToday.toInstant()

        // 获取Instant对应的时间戳
        return instant.toEpochMilli()
    }

    //获取今日北京时间的末尾时间戳，即今日23:59:59的时间戳
    fun getCurrentDayEndBeijingTimestamp(): Long {
        // 设置北京时间时区
        val beijingZone = ZoneId.of("Asia/Shanghai")

        // 获取当前日期，并创建今日23:59:59的LocalDateTime对象
        val endOfToday = LocalDateTime.now(beijingZone).withHour(23).withMinute(59).withSecond(59)

        // 将LocalDateTime转换为ZonedDateTime
        val zonedDateTimeEndOfToday = endOfToday.atZone(beijingZone)

        // 将ZonedDateTime转换为UTC的Instant
        val instantEndOfToday = zonedDateTimeEndOfToday.toInstant()

        // 获取Instant对应的时间戳
        return instantEndOfToday.toEpochMilli()
    }

}
